using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;

using System.Drawing.Imaging;
using System.Drawing.Printing;



namespace ArchSoft
{
    public partial class ExcelForm : Form
    {

        string PageSize = "A2";
        static int Stt = 1;
        static int HeadeLine = 5;

        private System.IO.Stream streamToPrint;
        string streamType;


        private string _folderPath;

        private string _folderName;

        private string _scale;

        DataRow row;
        DataColumn col;

        DataTable reportDT = new DataTable("Report");

        public string FolderPath
        {
            set { _folderPath = value; }
        }

        public string FolderName
        {
            set { _folderName = value; }
        }


        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt
        (

            IntPtr hdcDest, // handle to destination DC

            int nXDest, // x-coord of destination upper-left corner

            int nYDest, // y-coord of destination upper-left corner

            int nWidth, // width of destination rectangle

            int nHeight, // height of destination rectangle

            IntPtr hdcSrc, // handle to source DC

            int nXSrc, // x-coordinate of source upper-left corner

            int nYSrc, // y-coordinate of source upper-left corner

            System.Int32 dwRop // raster operation code
        );



        public ExcelForm()
        {
            InitializeComponent();
            //this.WindowState = FormWindowState.Maximized;

          //  GetTitle();

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Element";
            col.Caption = "Element";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "ID";
            col.Caption = "Drawing ID";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Number";
            col.Caption = "Drawing Number";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Name";
            col.Caption = "Drawing Name";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Scale";
            col.Caption = "Scale";
            reportDT.Columns.Add(col);

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "Size";
            col.Caption = "Paper Size";
            reportDT.Columns.Add(col);



            dataGridView1.DataSource = reportDT;

            //dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }


        public void NewLineForDrawing(string DrawingName, string Scale, int order)
        {
            try
            {
                string[] names = DrawingName.Split('_');
                names[1] = Path.GetFileNameWithoutExtension(names[1]);


                row = reportDT.NewRow();

                row["Element"] = _folderName;
                row["ID"] = names[0].Split('-')[0];
                row["Number"] = names[0].Split('-')[1];
                row["Name"] = names[1];
                row["Scale"] = Scale;
               // row["Size"] = PageSize;
                row["Size"] = PageSize;

                reportDT.Rows.Add(row);
            }
            catch (Exception ex)
            {

            }

           
        }

        public void NewBlankForDrawing()
        {
            row = reportDT.NewRow();

            row["Element"] = string.Empty;
            row["ID"] = string.Empty;
            row["Number"] = string.Empty;
            row["Name"] = string.Empty;
            row["Scale"] = string.Empty;
            // row["Size"] = PageSize;
            row["Size"] = string.Empty;

            reportDT.Rows.Add(row);
        }

        public string GetTitle()
        {
            string Pat = Directory.GetParent(this._folderPath).ToString() + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
            if (File.Exists(Pat))
            {
                //Load Noi dung File Dinh nghia(neu ton tai)!!!               
                System.Data.DataTable tbConsl = null;

                DataSet ds = new DataSet();
                ds.ReadXml(Pat);
                tbConsl = ds.Tables[0];
                //Load Consultant!
                if (tbConsl != null)
                {
                    //Gene!!    .TrimEnd(new char[] { '\0' });
                    Pat = EncodeDecode.GetDecoded(tbConsl.Rows[5][1].ToString()).Trim();
                    PageSize = EncodeDecode.GetDecoded(tbConsl.Rows[20][1].ToString()).Trim().Split(' ')[0];
                    if (Pat == "")
                        Pat = "NO NAME";
                }
                else
                    Pat = "NO NAME";
            }
            else
            {
                Pat = "NO NAME";
            }
            return Pat;
        }

        private void ExcelForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = reportDT;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);           

            int x = e.MarginBounds.X;

            int y = e.MarginBounds.Y;

            int width = image.Width;

            int height = image.Height;

            if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))

            {

                width = e.MarginBounds.Width;

                height = image.Height * e.MarginBounds.Width / image.Width;

            }

            else

            {

                height = e.MarginBounds.Height;

                width = image.Width * e.MarginBounds.Height / image.Height;

            }

            System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);

            e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
       }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Graphics g1 = this.CreateGraphics();

            Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);

            Graphics g2 = Graphics.FromImage(MyImage);

            IntPtr dc1 = g1.GetHdc();

            IntPtr dc2 = g2.GetHdc();

            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);

            g1.ReleaseHdc(dc1);

            g2.ReleaseHdc(dc2);

            MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);

            FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);

            StartPrint(fileStream, "Image");

            fileStream.Close();

            if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
            {

                System.IO.File.Delete(@"c:\PrintPage.jpg");

            }
        }

        public void StartPrint(Stream streamToPrint, string streamType)
        {

            this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

            this.streamToPrint = streamToPrint;

            this.streamType = streamType;

            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();

            PrintDialog1.AllowSomePages = true;

            PrintDialog1.ShowHelp = true;

            PrintDialog1.Document = printDocument1;

            DialogResult result = PrintDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {

                printDocument1.Print();

                //docToPrint.Print();

            }
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void printToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Graphics g1 = this.CreateGraphics();

            Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);

            Graphics g2 = Graphics.FromImage(MyImage);

            IntPtr dc1 = g1.GetHdc();

            IntPtr dc2 = g2.GetHdc();

            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);

            g1.ReleaseHdc(dc1);

            g2.ReleaseHdc(dc2);

            MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);

            FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);

            StartPrint(fileStream, "Image");

            fileStream.Close();

            if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
            {

                System.IO.File.Delete(@"c:\PrintPage.jpg");

            }
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.printPreviewControl1 = new PrintPreviewControl();
            //this.printPreviewControl1.Location = new Point(88, 80);
            //this.printPreviewControl1.Name = "PrintPreviewControl1";
            //this.printPreviewControl1.Dock = DockStyle.Fill;


            //printPreviewControl1.Document = this.printDocument1;
            //this.printPreviewControl1.Document.DocumentName = @"c:\PrintPage.jpg";

            //this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            //printPreviewControl1.Show();
        }

    }
}